package com.app.twth.ui.putaway;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityPutawayBinding;
import com.app.twth.ui.putaway.submit.PutawaySubmitActivity;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.FastJsonUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class PutawayActivity extends BaseActivity<ActivityPutawayBinding, PutawayViewModel> {
    private String TAG = getClass().getSimpleName();

    private PutawayDetails putawayDetails;
    private long timeMillis;
    private PutawayAdapter adapter;
    private List<Putatay> tallyList = new ArrayList<>();

    @Override
    protected void initListener() {
        viewModel.getPutawayContainer();
        viewModel.putawayContainerLiveData.observe(this, data -> {
            tallyList.clear();
            tallyList.addAll(data);
            adapter.setData(data);
        });
        viewModel.tallyDetailsLiveData.observe(this, data -> {
            tallyList = viewModel.putawayContainerLiveData.getValue();
            if (adapter != null) {
                adapter.setData(tallyList);
            }
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.skuListMutableLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showActivityForResult(mContext, PutawaySubmitActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected String setTitle() {
        return "Putaway(Container)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");

        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        adapter = new PutawayAdapter(this, tallyList, position -> {
            Putatay putatay = tallyList.get(position);
            if (tallyList.size() <= position) {
                return;
            }
            if (putatay.isShow()) {
                putatay.setShow(false);
                adapter.setData(tallyList);
                return;
            }
            bottomLayoutBinding.tvHint.setText("");
            viewModel.getPutawayDetail(position, putatay.getOrderId());
        }, (one, two) -> {
            if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                return;
            }
            timeMillis = System.currentTimeMillis();
            putawayDetails = tallyList.get(one).getContainerDetail().get(two);
            bottomLayoutBinding.tvHint.setText("");
            viewModel.getIbLpnSkuList(putawayDetails.getIbLpn());
        });
        binding.recyclerview.setHasFixedSize(true);
        binding.recyclerview.setItemAnimator(new DefaultItemAnimator());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layoutManager1);
        //添加装饰类
//        binding.recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerview.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
//                String container_no = binding.tvCode.getText().toString();
//                if (Utils.isEmpty(container_no)) {
//                    ToastUtil.showToast("Please enter container no.");
//                    return;
//                }
//                Tally tally1 = null;
//                for (Tally tally : tallyingList) {
//                    if (tally.getContainerNo().equalsIgnoreCase(container_no)) {
//                        tally1 = tally;
//                    }
//                }
//                if (tally1 == null) {
//                    ToastUtil.showToast("The container number does not exist or has been completed.");
//                    return;
//                }
//                showCheck(tally1);
//                getPresenter().checkContainerNo(container_no);
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
            return;
        }
        timeMillis = System.currentTimeMillis();
        bottomLayoutBinding.tvHint.setText("");
        viewModel.getIbLpnSkuList(barcodeStr);
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                EditTextUtil.hideInput(mContext);

                if (Utils.isEmpty(barcodeStr)) {
                    return false;
                }
                bottomLayoutBinding.tvHint.setText("");
                viewModel.getIbLpnSkuList(barcodeStr);
                return false;
            }
            return false;
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            Log.e(TAG, "onActivityResult");
            viewModel.getPutawayContainer();
        }
    }

}
